package com.boyiis.sell.controller;

import com.boyiis.sell.enums.ResultEnum;
import com.boyiis.sell.exception.SellException;
import com.boyiis.sell.model.dto.OrderDTO;
import com.boyiis.sell.model.entity.Product;
import com.boyiis.sell.model.entity.ProductCategory;
import com.boyiis.sell.model.form.ProductForm;
import com.boyiis.sell.service.OrderService;
import com.boyiis.sell.service.ProductCategoryService;
import com.boyiis.sell.service.ProductService;
import com.boyiis.sell.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/seller/product")
@Slf4j
public class SellerProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 商品列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
        PageRequest request = new PageRequest(page - 1, size);
        Page<Product> productPage = productService.findAll(request);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("productPage", productPage);
        map.put("currentPage", page);
        map.put("size", size);
        return new ModelAndView("product/list", map);

    }

    /**
     * 下架商品
     *
     * @param productId
     * @return
     */
    @GetMapping("/off_sale")
    public ModelAndView off_sale(@RequestParam(value = "productId") String productId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", "/sell/seller/product/list");

        try {
            productService.offSale(productId);
        } catch (Exception e) {
            log.error("【卖家下架商品】 {}", e.getMessage());
            map.put("msg", e.getMessage());
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.SUCCESS.getMsg());
        return new ModelAndView("common/success", map);

    }

    /**
     * 上架商品
     *
     * @param productId
     * @return
     */
    @GetMapping("/on_sale")
    public ModelAndView on_sale(@RequestParam(value = "productId") String productId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", "/sell/seller/product/list");

        try {
            productService.onSale(productId);
        } catch (Exception e) {
            log.error("【卖家上架商品】 {}", e.getMessage());
            map.put("msg", e.getMessage());
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.SUCCESS.getMsg());
        return new ModelAndView("common/success", map);

    }

    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId", required = false) String productId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", "/sell/seller/product/list");

        if (StringUtils.isNotBlank(productId)) {
            Product product = productService.findOne(productId);
            map.put("product", product);
        }

        //查询所有的类目
        List<ProductCategory> productCategoryList = productCategoryService.findAll();
        map.put("productCategoryList", productCategoryList);

        return new ModelAndView("product/index", map);

    }

    /**
     * 保存更新
     *
     * @param form
     * @param bindingResult
     * @return
     */
    @PostMapping("/save")
    public ModelAndView save(@Valid ProductForm form, BindingResult bindingResult) {
        Map<String, Object> map = new HashMap<String, Object>();

        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/product/index");
            return new ModelAndView("common/error", map);
        }

        try{
            Product product = new Product();
            if(StringUtils.isNotBlank(form.getProductId())){
                product = productService.findOne(form.getProductId());
            }else{
                form.setProductId(KeyUtil.genUniqueKey());
            }

            BeanUtils.copyProperties(form, product);
            product = productService.save(product);
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", e.getMessage());
            if(StringUtils.isBlank(form.getProductId())){
                map.put("url", "/sell/seller/product/index");
            }else{
                map.put("url", "/sell/seller/product/index?productId=" + form.getProductId());
            }

            return new ModelAndView("common/error", map);
        }

        map.put("url", "/sell/seller/product/list");
        return new ModelAndView("common/success", map);

    }


}
